var ajax = require('../../utils/ajax.js');
var wxUtil = require('../../utils/wxUtil.js');
var util = require('../../utils/util.js');
var shopInfo = require('../../components/shopInfo/index.js');
var common = require('../../utils/common.js');
var MPage = require('../MPage.js');
var app = getApp();

Page(common.extendPage(MPage, {
    data: {
        dataLoaded: false,
        orderId: '',
        shop: {},
        order: {},
        totalPrice: '',
        logisticsNumber: ''
    },

    onLoad: function(options) {
        var {
            orderId,
            actType,
        } = this.options;

        this.setData({
            orderId,
            actType,
        });
        this._initPage();
    },

    _initPage: function() {
        var _self = this;
        var {
            orderId,
            actType
        } = this.data;

        ajax.P('/openapi/CHome/orderDetail', {
            orderId,
            actType,
        }, function(data) {
            var order = data.result.order

            shopInfo.init('shopInfoData', _self);
            _self.setData({
                dataLoaded: true,
                order: order,
                logisticsNumber: order.expressNum,
                shop: app.getShop(),
                groupBuyState: order.groupBuyState,
            });
        });
    },

    /**
     * 未支付订单支付
     */
    _payOrder: function(evt) {
        var formId = evt.detail.formId;
        var _self = this;
        var {
            orderId,
            actType
        } = this.data;
        var url = '/openapi/CHome/doPayAgain';

        if (actType == 1) { //团购
            url = '/openapi/CActivity/doPayAgain';
        }
        ajax.P(url, {
            orderId,
            openId: app.getOpenId(),
        }, function(data) {
            var wxPayParams = JSON.parse(data.result.wxPayParams);
            _self._package = wxPayParams.package;
            var prepayArr = _self._package.split('=');
            common.submitPrepay(prepayArr[1]);

            wxPayParams.success = function() {
                wx.redirectTo({
                    url: '/pages/orderList/orderList?ty=1'
                });
            }
            wxUtil.wxPay(wxPayParams);
        });
        common.submitForm(formId);
    },

    /**
     * 申请退款
     */
    // _applyRefund: function () {
    //   var self = this;

    //   ajax.P(
    //     '/openapi/CHome/applyOrderRefund',
    //     {
    //       orderId: this.data.orderId,
    //     },
    //     function (data) {
    //       util.alert('您已申请退款，请耐心等待商家处理', function () {
    //         wx.redirectTo({ url: '/pages/orderDetail/orderDetail?orderId=' + self.data.orderId });
    //       });
    //     });
    // },

    _applyRefund: function(evt) {
        var formId = evt.detail.formId;
        var {
            actType,
            groupBuyState
        } = this.data;

        common.submitForm(formId);

        if (actType == 1 && groupBuyState != 1) { //拼团且未成功
            return util.alert('拼团成功方才可申请退款!');
        }
        util.alert('请联系商家退款!');
    },

    _goToLogistics: function(evt) {
        var {
            logisticsNumber,
            orderId,
            order
        } = this.data;
        var orderType = order.order_type;
        var deliveryId = order.delivery_id;

        wx.navigateTo({
                url: '/pages/orderExpressInfo/orderExpressInfo?expressNumber=' + logisticsNumber + '&orderId=' + orderId + '&orderType=' + orderType + '&deliveryId=' + deliveryId
            })
            // common.jump();
    },

    /**
     * 确认收货
     * @return {[type]} [description]
     */
    _confirmTakeGoods(evt) {
        var _self = this;
        var formId = evt.detail.formId;

        util.confirm('您是否确定收货？', function() {
            ajax.P('/openapi/CHome/confirmTakeDelivery', {
                orderId: _self.data.orderId,
            }, function(data) {
                wx.redirectTo({
                    url: '/pages/orderDetail/orderDetail?orderId=' + _self.data.orderId
                })
            });
        });
        common.submitForm(formId);
    },
    _jump: function(evt) {
        common.jump(evt);
    }
}));